package com.anxinr.controller;

import com.anxinr.pojo.entity.Goods;
import com.anxinr.pojo.entity.TestCase;
import com.anxinr.result.Result;
import com.anxinr.service.IGoodsService;
import com.anxinr.service.ITestCaseService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author anxinr
 */
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService ;

    @PostMapping("/query")
    public Result<IPage<Goods>> query(@RequestBody Goods goods){
        return new Result(goodsService.queryUsers(goods)) ;
    }

    @PostMapping("/save")
    public Result save(@RequestBody Goods goods) throws Exception {
        try {
            return new Result(goodsService.save(goods)) ;
        }catch (Exception e){
            e.printStackTrace();
            throw e ;
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Goods goods) throws Exception {
        try {
            return new Result(goodsService.updateById(goods)) ;
        }catch (Exception e){
            e.printStackTrace();
            throw e ;
        }
    }

    @GetMapping("/remove")
    public Result remove(@RequestParam("iname") String iname){
        try {
            LambdaUpdateWrapper<Goods> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Goods::getIname,iname);
            goodsService.remove(wrapper);
            return Result.success() ;
        }catch (Exception e){
            e.printStackTrace();
            throw e ;
        }
    }
    
}
